import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'

function createWindow(): void {
  // Create the browser window.
  // 窗口配置
  const mainWindow = new BrowserWindow({
    width: 900,
    height: 670,
    show: false,
    frame: false, //无边框窗口
    resizable: false, //无法调整窗口大小
    // titleBarStyle: 'hidden',//隐藏标题栏
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
    },
  })

  mainWindow.on('ready-to-show', () => {
    mainWindow.show()
  })

  // 窗口拖拽
  ipcMain.handle('custom-adsorption', (event, res) => {
    let x = res.appX
    let y = res.appY
    mainWindow.setPosition(x, y)
  })

  // 关闭软件
  ipcMain.handle('close-login', () => {
    // 关闭窗口
    mainWindow.close() //可以监听close事件来执行一些自定义操作，并有机会取消关闭操作
    // mainWindow.destroy()  // 方法用于彻底销毁窗口，不会触发close事件，并立即释放与窗口相关所有信息
    // 关闭软件
    // app.quit() //关闭软件 可以通过监听before-quit事件来执行预处理操作
    // app.exit() //退出软件 不会触发任何声明周期事件
  })

  // 进入管理系统首页
  ipcMain.handle('resize-window', () => {
    //从login跳转到Home，只是路由的切换，窗口并没有换，所以窗口的配置会一直生效，那么就要修改窗口的：

    //窗口大小
    mainWindow.setSize(1200, 720)
    //窗口最小值
    mainWindow.setMinimumSize(1000, 500)
    //窗口居中
    mainWindow.center()
    //窗口大小可以修改
    mainWindow.setResizable(true)
  })

  // 退出登录
  ipcMain.handle('out-login', () => {
    //窗口大小
    mainWindow.setSize(900, 670)
    //窗口最小值
    mainWindow.setMinimumSize(900, 500)
    //窗口居中
    mainWindow.center()
    //窗口大小可以修改
    mainWindow.setResizable(true)
  })

  // 关闭应用
  ipcMain.handle('win-close', () => {
    app.exit()
  })

  // 最小化
  ipcMain.handle('win-min', () => {
    mainWindow.minimize()
  })

  // 最大化
  ipcMain.handle('win-max', () => {
    if (mainWindow.isMaximized()) {
      mainWindow.unmaximize()
    } else {
      mainWindow.maximize()
    }
  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron')

  // Default open or close DevTools by F12 in development
  // and ignore CommandOrControl + R in production.
  // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  // IPC test
  ipcMain.on('ping', () => console.log('pong'))

  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
